package com.ftg.learn.chapter08.pattern.singleton.thread;

/**
 * 账户
 *
 * 经典问题：我们创建一个银行账户类，其中有一个共享的balance变量表示账户余额。
 * 两个线程将同时进行存款和取款操作。？
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/2 19:25
 */

public class AccountTest02 {
    /**
     * deposit   存款
     * withdraw  取款
     */

    private static int balance = 0;

    private static void withdraw() {
        balance -= 100;
    }

    private static void deposit() {
        balance += 1000;
    }

    public static void main(String[] args) {
        Thread t1 = new Thread(()-> {
        if (balance <= 0) {
            System.out.println("存款为0，不能取款");
        } else {
            withdraw();
        }
    });

        Thread t2 = new Thread(()-> {
            deposit();
        });

        t1.start();
        t2.start();

        try{
            t1.join();
            t2.join();
        }catch (Exception e){
            System.out.println("出错了");
        }

        System.out.println(balance);
    }

}
